﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace 文件浏览器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //递归添加目录节点
        private void AddDirectoryNodes(TreeNode node,DirectoryInfo info)
        {
            try
            {
                foreach (DirectoryInfo subDir in info.GetDirectories())
                {   //对于每一个子节点
                    TreeNode subNode = new TreeNode(subDir.Name);
                    subNode.Tag = subDir;
                    node.Nodes.Add(subNode);
                    AddDirectoryNodes(subNode, subDir); // 递归添加子目录
                }
            }
            catch (UnauthorizedAccessException ex) {
              //不处理权限问题
            } 
        }

        private void InitialTreeView1()
        {
            // 清除现有的节点
            treeView1.Nodes.Clear();

            // 获取C盘根目录信息
            DirectoryInfo cDrive = new DirectoryInfo("C:\\");
            TreeNode rootNode = new TreeNode(cDrive.Name);
            rootNode.Tag = cDrive;
            AddDirectoryNodes(rootNode, cDrive);
            treeView1.Nodes.Add(rootNode);

            // 设置TreeView的AfterSelect事件处理器
          //  treeView1.AfterSelect += treeView1_AfterSelect;
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {   //刷新右侧视图
            listView1.Items.Clear();

            if (e.Node.Tag is DirectoryInfo selectedDir)
            {
                var files = selectedDir.GetFiles();
                var directories = selectedDir.GetDirectories();

                // 添加文件
                foreach (var file in files)
                {
                    ListViewItem item = new ListViewItem(file.Name, 0);
                    item.SubItems.Add(file.Extension);
                    item.Tag = file.FullName;
                    listView1.Items.Add(item);
                }

                // 添加子文件夹
                foreach (var dir in directories)
                {
                    ListViewItem item = new ListViewItem(dir.Name, 1);
                    item.SubItems.Add("<Folder>");
                    item.Tag = dir.FullName;
                    listView1.Items.Add(item);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitialTreeView1();
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                //获取选中文件路径
                string filePath = listView1.SelectedItems[0].Tag.ToString();
                ProcessStartInfo psi;
                //exe后缀文件
                if (filePath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                {
                    psi = new ProcessStartInfo(filePath);
                }//txt后缀文件，用记事本打开
                else if (filePath.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    psi = new ProcessStartInfo("notepad.exe", filePath);
                }
                else
                {
                    return; // 不支持其他类型的文件
                }
                Process.Start(psi);
            }
            
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            InitialTreeView1();
            listView1.Clear();
        }
    }
}
